import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'

const LOCAL = "http://localhost:8080"
const TEST1 = 'http://218.17.162.120/rfid/'
const TEST2 = 'http://218.17.162.120:9096/rfid/'
const TEST3 = '/agent/'
const PRODUCT = 'http://192.168.1.108/rfid/'
const HOST = TEST3

const API_LOGIN = 'signIn'
const API_LOGOUT = 'logOut'
const API_UPDATE_PASSWORD = 'updatePassword'
const API_GET_COMPANY = 'api/company/'

const API_ASSETS = 'api/assets/'
const API_MOVE_ASSETS = 'api/assets/move'
const API_ASSETS_BY_NAME = 'api/assets/name/'
const API_ASSETS_BY_ID = 'api/assets/id/'
const API_ASSETS_BY_AREA_ID = 'api/assets/areaIdAndUserId/'
const API_ALARM_SWITCH = 'api/assets/alarmSwitch'
const API_ASSETS_JSON_CHECK = 'api/assets/insertAssets'

const API_LABELS = 'api/tags/'
const API_MOVE_LABELS = 'api/assets/changeTag'
const API_LABELS_BY_ID = 'api/tags/id'

const API_AREAS = 'api/areas/'
const API_AREAS_BY_ID = 'api/areas/id'
const API_AREAS_BY_NAME = 'api/areas/name'
const API_AREAS_UPLOAD = 'api/areas/uploadExcel'
const API_AREAS_JSON_CHECK = 'api/areas/insertAreas'

const API_READER = 'api/reader/'
const API_MOVE_READER = 'api/reader/move'
const API_READER_BY_ID = 'api/reader/id/'
const API_READER_BAND_AREA = 'api/reader/bandArea'

axios.defaults.baseURL = HOST

axios.defaults.headers.put['Content-Type'] = 'application/json'
axios.defaults.withCredentials = true
axios.interceptors.request.use(function (config) {
    if (config.ContentType !== 'application/json' &&config.method !== 'get' && config.method !== 'put') {
        config.data = qs.stringify(config.data)
    }
    return config
})

Vue.prototype.$ajax = {

    //登录请求
    autoService: {
        login(params) {
            return axios({
                method: 'POST',
                url: API_LOGIN,
                cache: true,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
        logout(params) {
            return axios({
                method: 'POST',
                url: API_LOGOUT,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        }
    },

    //账户信息
    accountData: {
        updatePwd(params) {
            return axios({
                method: 'POST',
                url: API_UPDATE_PASSWORD,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
        getCompany(id) {
            return axios({
                method: 'GET',
                url: API_GET_COMPANY + id,
            })
        },
        updateCompany(params) {
            return axios({
                method: 'PUT',
                url: API_GET_COMPANY,
                data: params,
            })
        }
    },

    //固定资产
    assets: {
        getAssets(params) {
            return axios({
                method: 'GET',
                url: API_ASSETS,
                params: params
            })
        },
        getAssetsByName(params) {
            return axios({
                method: 'GET',
                url: API_ASSETS_BY_NAME,
                params: params
            })
        },
        getAssetsById(params) {
            return axios({
                method: 'GET',
                url: API_ASSETS_BY_ID,
                params: params
            })
        },
        getAssetsByAreaId(params) {
            return axios({
                method: 'GET',
                url: API_ASSETS_BY_AREA_ID,
                params: params
            })
        },
        deleteAssets(ids) {
            return axios({
                method: 'DELETE',
                url: API_ASSETS,
                data: ids,
                ContentType: 'application/json'
            })
        },
        moveAssets(params) {
            return axios({
                method: 'PATCH',
                url: API_MOVE_ASSETS,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
        updateAssets(params) {
            return axios({
                method: 'PUT',
                url: API_ASSETS,
                data: params
            })
        },
        alarmSwitch(params) {
            return axios({
                method: 'PATCH',
                url: API_ALARM_SWITCH,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
        jsonCheck(params) {
            return axios({
                method: 'POST',
                url: API_ASSETS_JSON_CHECK,
                data: params,
                ContentType: 'application/json'
            })
        },
    },

    labels: {
        getLabels(params) {
            return axios({
                method: 'GET',
                url: API_LABELS,
                params: params
            })
        },
        getLabelsById(params) {
            return axios({
                method: 'GET',
                url: API_LABELS_BY_ID,
                params: params
            })
        },
        addLabels(ids) {
            return axios({
                method: 'POST',
                url: API_LABELS,
                data: ids,
                ContentType: 'application/json'
            })
        },
        deleteLabels(ids) {
            return axios({
                method: 'DELETE',
                url: API_LABELS,
                data: ids,
                ContentType: 'application/json'
            })
        },
        moveLabels(params) {
            return axios({
                method: 'POST',
                url: API_MOVE_LABELS,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
    },

    areas: {
        getAreas(params) {
            return axios({
                method: 'GET',
                url: API_AREAS,
                params: params
            })
        },
        getAreasById(params) {
            return axios({
                method: 'GET',
                url: API_AREAS_BY_ID,
                params: params
            })
        },
        getAreasByName(params) {
            return axios({
                method: 'GET',
                url: API_AREAS_BY_NAME,
                params: params
            })
        },
        updateAreas(params) {
            return axios({
                method: 'PUT',
                url: API_AREAS,
                data: params
            })
        },
        deleteAreas(ids) {
            return axios({
                method: 'DELETE',
                url: API_AREAS,
                data: ids
            })
        },
        uploadAreas(params) {
            return axios({
                method: 'POST',
                url: API_AREAS_UPLOAD,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
        jsonCheck(params) {
            return axios({
                method: 'POST',
                url: API_AREAS_JSON_CHECK,
                data: params,
                ContentType: 'application/json'
            })
        },
    },

    readers: {
        getReaders(params) {
            return axios({
                method: 'GET',
                url: API_READER,
                params: params
            })
        },
        getReadersById(params) {
            return axios({
                method: 'GET',
                url: API_READER_BY_ID,
                params: params
            })
        },
        deleteAreas(ids) {
            return axios({
                method: 'DELETE',
                url: API_READER,
                data: ids
            })
        },
        moveAreas(params) {
            return axios({
                method: 'PATCH',
                url: API_MOVE_READER,
                data: params,
                ContentType: 'application/x-www-form-urlencoded'
            })
        },
        bandArea(params) {
            return axios({
                method: 'PATCH',
                url: API_READER_BAND_AREA,
                data: params,
                ContentType: 'application/json'
            })
        },
    }
}

Vue.prototype.$msgDictionary = {
    operation: {
        suc: '操作成功！',
        fail: '操作失败！'
    },
    subscribe: {
        suc: '订阅成功！',
        fail: '订阅失败！'
    },
    add: {
        suc: '添加成功！',
        fail: '添加失败！',
        emptyTags: '请先录入电子标签编号！',
        emptyReaders: '请先录入阅读器编号！',
        assets:{
            emptyName: '资产名称必填！',
            emptyId: '资产编号必填！',
            isNaN: '资产编号必须为数字！',
            emptyPersonLiable: '责任人必填！',
        },
        readers:{
            isNaN: '阅读器编号必须全为数字！',
        },
        tags:{
            isNaN: '电子标签编号必须全为数字！',
        }
    },
    delete: {
        suc: '删除成功！',
        fail: '删除失败！'
    },
    update: {
        suc: '修改成功！',
        fail: '修改失败！'
    },
    upload: {
        suc: '上传成功！',
        fail: '上传失败！',
        fix: '还有数据不合法，请修改！'
    },
    query: {
        emptyInput: '查询条件不能为空！',
        emptyResult: '未查询到相应数据!',
        emptySelect: '请先选择查询类型'
    },
    email: {
        errorEmail: '邮箱格式错误！',
    },
    pwd: {
        oldEmpty: '旧密码不能为空！',
        newEmpty: '新密码不能为空！',
        repeated: '新旧密码不能相同！',
        different: '两次输入的密码不一样！',
        format: '新密码须大于等于6位切小于等于20位!'
    },
    loginEmpty: "账号或密码不能为空！",
    serviceException: '服务器异常,请联系管理员！',
    chooseFirst: "请先选择！",
    loginTimeout: "登陆超时，请重新登陆！"
}
